package top.soulblack.purchasesale.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.soulblack.purchasesale.mapper.GoodsClassifyMapper;
import top.soulblack.purchasesale.model.GoodsClassify;

import java.util.*;


@Service
public class GoodsClassifyService {
    @Autowired
    private GoodsClassifyMapper goodsClassifyMapper;
    private List<GoodsClassify> goodsClassifies = new ArrayList<GoodsClassify>();

    public int insert(GoodsClassify goodsClassify) {
        return goodsClassifyMapper.insert(goodsClassify);
    }

    public List<GoodsClassify> selectAll() {
        return goodsClassifyMapper.selectAll();
    }

    public GoodsClassifyService(){
        super();
    }



    public GoodsClassifyService(List<GoodsClassify> goodsClassifies){
        super();
        this.goodsClassifies = goodsClassifies;
    }

    public String test(){
        return buildTree(selectAll());
    }

    public String buildTree(List<GoodsClassify> goodsClassify) {

        GoodsClassifyService goodsClassifyService = new GoodsClassifyService(goodsClassify);

        return goodsClassifyService.buildJSONTree();
    }

    public String buildJSONTree(){
        List<GoodsClassify> goodsClassifiesTree = buildTree();
        JSONArray jsonArray = JSONArray.parseArray(JSON.toJSONString(goodsClassifiesTree));
        return jsonArray.toString();
    }

    public List<GoodsClassify> buildTree(){
        List<GoodsClassify> treeNodes = new ArrayList<>();
        List<GoodsClassify> rootNodes = getRootNodes();
        for (GoodsClassify rootNode : rootNodes) {
            buildChildNodes(rootNode);
            treeNodes.add(rootNode);
        }
        return treeNodes;
    }

    /**
     * @Author li
     * @Description 将子节点放置于父节点中
     * @Date 10:12 2019/6/18
     * @Param [goodsClassify]
     * @return void
     **/
    public void buildChildNodes(GoodsClassify goodsClassify) {
        List<GoodsClassify> children = getChildNodes(goodsClassify);
        if (!children.isEmpty()) {
            for (GoodsClassify child : children) {
                //树状搜索
                buildChildNodes(child);
            }
            goodsClassify.setChildren(children);
        }
    }

    /***
     * @Author li
     * @Description 获取子节点，pid与id相同为子项
     * @Date 10:10 2019/6/18
     * @Param [parentGoodClassify]
     * @return java.util.List<top.soulblack.purchasesale.model.GoodsClassify>
     */
    public List<GoodsClassify> getChildNodes(GoodsClassify parentGoodClassify) {
        List<GoodsClassify> childNodes = new ArrayList<>();
        for (GoodsClassify n : goodsClassifies) {
            if (parentGoodClassify.getId().equals(n.getpId())) {
                childNodes.add(n);
            }
        }
        return childNodes;
    }

    /**
     * @Author li
     * @Description 判断是否为根节点，根节点pid设置为0
     * @Date 10:27 2019/6/18
     * @Param [goodsClassify]
     * @return boolean
     **/
    public boolean rootNode(GoodsClassify goodsClassify) {
        boolean isRootNode = false;
        if(goodsClassify.getpId().equals(0))
            isRootNode = true;
        return isRootNode;
    }

    /**
     * @Author li
     * @Description 获取根节点列表
     * @Date 10:30 2019/6/18
     * @Param []
     * @return java.util.List<top.soulblack.purchasesale.model.GoodsClassify>
     **/
    public List<GoodsClassify> getRootNodes() {
        List<GoodsClassify> rootNodes = new ArrayList<>();
        for (GoodsClassify n : goodsClassifies) {
            if (rootNode(n)) {
                rootNodes.add(n);
            }
        }
        return rootNodes;
    }

    public List<String> selectGroups() {
        return goodsClassifyMapper.selectGroups();
    }

    public void addClassify(GoodsClassify goodsClassify) {
        goodsClassifyMapper.addClassify(goodsClassify);
        return;
    }

    public boolean existClassify(String name) {
        int num  = goodsClassifyMapper.existClassify(name);
        if (num == 1) {
            return true;
        }
        return false;
    }

    public boolean deleteClassify(Integer classifyId) {
        List<Integer> num = goodsClassifyMapper.deleteById(classifyId);
        if (num != null) {
            return true;
        }
        return false;
    }

    public boolean updateClassify(GoodsClassify goodsClassify) {
        int num = goodsClassifyMapper.updateByPrimaryKey(goodsClassify);
        if (num == 1) {
            return true;
        }
        return false;
    }


    public List<GoodsClassify> selectForGroup(String group) {
        return goodsClassifyMapper.selectForGroup(group);
    }

    public boolean updateGroup(String newGroup, String oldGroup) {
        int num = goodsClassifyMapper.updateGroup(newGroup, oldGroup);
        if (num >= 1) {
            return true;
        }
        return false;
    }

    public boolean deleteGroup(String oldGroup) {
        int num = goodsClassifyMapper.deleteGroup(oldGroup);
        if (num >= 1) {
            return true;
        }
        return false;
    }
}


